package cn.zk.design.pub_sub;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.utils.ZKPaths;

/**
 * ClassName: Pub
 * Desc:
 * Date： 2016/12/7
 * Created：shaom
 */
public class Pub {

    public static final String connectString = "master:2181,slave01:2181,slave02:2181";
    public static final String path = "/Pub_Sub/db_config";

    static CuratorFramework client = CuratorFrameworkFactory.builder().connectString(connectString)
            .sessionTimeoutMs(5000)
            .retryPolicy(new ExponentialBackoffRetry(2000, 5))
            .build();

    public static final String driverClassName = "dbcp.driverClassName";
    public static final String dbJDBCUrl = "dbcp.dbJDBCUrl";
    public static final String userName = "dbcp.userName";
    public static final String password = "dbcp.password";

    static String path1 = ZKPaths.makePath(path, driverClassName);
    static String path2 = ZKPaths.makePath(path, dbJDBCUrl);
    static String path3 = ZKPaths.makePath(path, userName);
    static String path4 = ZKPaths.makePath(path, password);

    public static final String driverClassNameValue = "com.mysql.jdbc.Driver";
    public static final String dbJDBCUrlValue = "jdbc:mysql://1.1.1.1:3306/taoKeeper";
    public static final String userNameValue = "xiMing";
    public static final String passwordValue = "123456";

    public static void main(String[] args) throws Exception {
        client.start();
//        init();
        pub();
    }

    static void init() throws Exception {
        client.delete().deletingChildrenIfNeeded().forPath(path);

        client.create().creatingParentsIfNeeded().forPath(path1, driverClassNameValue.getBytes());
        client.create().creatingParentsIfNeeded().forPath(path2, dbJDBCUrlValue.getBytes());
        client.create().creatingParentsIfNeeded().forPath(path3, userNameValue.getBytes());
        client.create().creatingParentsIfNeeded().forPath(path4, passwordValue.getBytes());
    }

    static void pub() throws Exception {
        client.setData().forPath(path1, "change".getBytes());
        client.setData().forPath(path2, "change".getBytes());
        client.setData().forPath(path3, "change".getBytes());
        client.setData().forPath(path4, "change".getBytes());
    }
}
